# Read in the candidate data with clustering results
cand1990 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn1990.csv",stringsAsFactors=F)
cand1992 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn1992.csv",stringsAsFactors=F)[,-c(14:16)]
cand1994 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn1994.csv",stringsAsFactors=F)
cand1996 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn1996.csv",stringsAsFactors=F)
cand1998 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn1998.csv",stringsAsFactors=F)
cand2000 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn2000.csv",stringsAsFactors=F)
cand2002 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn2002.csv",stringsAsFactors=F)
cand2004 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn2004.csv",stringsAsFactors=F)
cand2006 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn2006.csv",stringsAsFactors=F)
cand2008 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/DynamicClustering/ClusteringResultsDyn2008.csv",stringsAsFactors=F)

candDat <- rbind(cand1990,cand1992,cand1994,cand1996,cand1998,cand2000,cand2002,cand2004,cand2006,cand2008)

t <- 2008
dates <- sort(c(as.Date(paste(t-1,"-",1:12,"-",1,sep="")),as.Date(paste(t,"-",1:12,"-",1,sep=""))))[3:23]

library(foreign)
electionDat <- read.dta("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/Jacobson 1946 to 2008.dta")

electionDat <- subset(electionDat,electionDat$year>=1990)
icpsrMatch <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Data/icpsrMatch.csv",stringsAsFactors=F)[,c(1,2)]

electionDat$stabbr <- icpsrMatch[match(electionDat$st_icpsr,icpsrMatch[,1]),2]

#### Code 'modal cluster' ###
Mode <- function(v){
vt <- table(v)
as.numeric(names(vt[vt == max(vt)])) 
}

modeClust <- matrix(0,nrow(candDat),21)
for(t in unique(candDat$Cycle)){
	dati <- subset(candDat,candDat$Cycle==t)
	for(c in 14:34){
		modec <- Mode(dati[,c])
		modeClust[which(candDat$Cycle==t),c-13] <- candDat[which(candDat$Cycle==t),c] == modec
	}
	print(t)
}

colnames(modeClust) <- paste("mcT",1:21,sep="")
modeClust = modeClust == 0
modeClust[is.na(modeClust)] <- 0

yrStDist <- paste(candDat$Cycle,substr(candDat$DistIDRunFor,1,2),as.numeric(substr(candDat$DistIDRunFor,3,4)),sep="")
electionDat$yrStDist <- paste(electionDat$year,electionDat$stabbr,electionDat$cd,sep="")
candDat$yrStDist <- yrStDist

## Add cluster data to election data ##
DBClust <- matrix(0,nrow(electionDat),21)
RBClust <- matrix(0,nrow(electionDat),21)
RInc <- numeric(nrow(electionDat))
Dinc <- numeric(nrow(electionDat))
for(i in 1:nrow(electionDat)){
	#candDatt <- subset(candDat,candDat$Cycle==electionDat$year[i])
	dclusti <- subset(modeClust,candDat$yrStDist==electionDat$yrStDist[i] & candDat$Party=="D")
	if(length(dclusti)>0){
		DBClust[i,] <- apply(rbind(dclusti),2,sum) > 0
		}
	rclusti <-  subset(modeClust,candDat$yrStDist==electionDat$yrStDist[i] & candDat$Party=="R")
	if(length(rclusti)>0){
	RBClust[i,] <- apply(rbind(rclusti),2,sum) > 0
	}
}

RBClust[which(is.na(RBClust))] <- 0
DBClust[which(is.na(DBClust))] <- 0

#electionDat <- cbind(electionDat,RBClust,DBClust)


quintile <- function(x,slices=16){
	quints <- quantile(x,prob=seq(0,1,length=slices+2),na.rm=T)[-c(1,slices+2)]
	qtile <- 1
	for(i in 1:length(quints)){
		qtile <- qtile + (x>quints[i])
	}
	qtile
}


InOut <- matrix(0,8,21)
OutIn <- matrix(0,8,21)
for(e in seq(1994,2008,by=2)){
for(m in 11:21){
electionDat$RBClust <- RBClust[,m]
electionDat$DBClust <- DBClust[,m]


outin <- mean( ((DBClust[,m]*(electionDat$inc==0)+RBClust[,m]*(electionDat$inc==1))!=(DBClust[,m-1]*(electionDat$inc==0)+RBClust[,m-1]*(electionDat$inc==1)) )[electionDat$year==e])

inout <- sum( ((1-DBClust[,m]*(electionDat$inc==0)+RBClust[,m]*(electionDat$inc==1))*(DBClust[,m-1]*(electionDat$inc==0)+RBClust[,m-1]*(electionDat$inc==1)) )[electionDat$year==e])

InOut[(e-1992)/2,m] <- inout
OutIn[(e-1992)/2,m] <- outin
}
}

plot(dates[11:21],OutIn[1,11:21],ylim=c(0,.1))
for(i in 2:8){
	lines(dates[11:21],OutIn[i,11:21])
}

plot(dates[11:20],InOut[1,11:20],ylim=c(0,50), type="l")
for(i in 2:8){
	lines(dates[11:20],InOut[i,11:20])
}

plot(dates[11:21],OutIn[1,11:21],ylim=c(0,.15), type="l")
for(i in 2:8){
	lines(dates[11:21],OutIn[i,11:21])
}


filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Tex/","chalChurn.pdf",sep="")
pdf(filet,width=6,heigh=4,pointsize=14)
par(las=1,mar=c(2,4,1,1))
plot(dates[11:21],OutIn[1,11:21],col=gray(1-1/8),ylim=c(0,.15), type="l", ylab="Month-to-Month Change Rate",xlab="",lwd=2)
for(i in 2:8){
	lines(dates[11:21],OutIn[i,11:21],col=gray(1-i/8),lwd=2)
}
dev.off()

filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Active/Camp/Tex/","chalAdd.pdf",sep="")
pdf(filet,width=6,heigh=4,pointsize=14)
par(las=1,mar=c(2,4,1,1))
plot(dates[11:20],OutIn[1,11:20],col=gray(1-1/8),ylim=c(0,50), type="l", ylab="Added Networked Support",xlab="",lwd=2)
for(i in 2:8){
	lines(dates[11:20],OutIn[i,11:20],col=gray(1-i/8),lwd=2)
}
dev.off()

